/**
 * 墨迹天气 api实现接口
 */
const axios = require('axios');

// 天气云请求头
const header = {
  'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  'Authorization': 'APPCODE 7cbac5a97d35426b9cf849ec79e4d661'
}

// 天气云请求的经纬度（大连市沙河口区）
const latLon = 'lat=38.94382049&lon=121.5765993';

/**
 * 获取天气云的天气实况
 * @returns 
 */
async function getTempOnTime() {
  // 获取天气云的天气实况
  const url = "http://aliv8.data.moji.com/whapi/json/aliweather/condition";
  let re;
  await axios.post(url, latLon, { headers: header })
    .then(function (res) {
      if (res && res.data.code == 0 && res.data.data) {
        re = res.data.data.condition;
      }
      // console.log(response.data);
    })
    .catch(function (error) {
      console.log("---------------getBaseTemp start-------------------");
      console.log(error);
      console.log("---------------getBaseTemp end-------------------");
      re = {};
    })

    return re;
}

/**
 * 获取天气云的空气质量
 * @returns 
 */
async function getAQI() {
  // 获取天气云的空气质量
  const url = "http://aliv8.data.moji.com/whapi/json/aliweather/aqi";

  let re;
  await axios.post(url, latLon, { headers: header })
    .then(function (res) {
      if (res && res.data.code == 0 && res.data.data) {
        re = res.data.data.aqi;
      }
      // console.log(response.data);
    })
    .catch(function (error) {
      console.log("---------------getAQI start-------------------");
      console.log(error);
      console.log("---------------getAQI end-------------------");
      re = {};
    })
    return re;
}

/**
 * 获取天气云的生活指数
 * @returns 
 */
async function getIndex() {
  // 获取天气云的生活指数
  const url = "http://aliv8.data.moji.com/whapi/json/aliweather/index";

  let re;
  await axios.post(url, latLon, { headers: header })
    .then(function (res) {
      if (res && res.data.code == 0 && res.data.data) {
        re = res.data.data.liveIndex;
      }
      // console.log(response.data);
    })
    .catch(function (error) {
      console.log("---------------getIndex start-------------------");
      console.log(error);
      console.log("---------------getIndex end-------------------");
      re = {};
    })
    return re;
}

/**
 * 获取天气云的24小时天气
 * @returns 
 */
async function getTwentyFourHours() {
  // 获取天气云的24小时天气
  const url = "http://aliv8.data.moji.com/whapi/json/aliweather/forecast24hours";

  let re;
  await axios.post(url, latLon, { headers: header })
    .then(function (res) {
      if (res && res.data.code == 0 && res.data.data) {
        re = res.data.data.hourly;
      }
      // console.log(response.data);
    })
    .catch(function (error) {
      console.log("---------------getTwentyFourHours start-------------------");
      console.log(error);
      console.log("---------------getTwentyFourHours end-------------------");
      re = {};
    })
    return re;
}

/**
 * 获取天气云的预警
 * @returns 
 */
async function getAlert() {
  // 获取天气云的预警
  const url = "http://aliv8.data.moji.com/whapi/json/aliweather/alert";

  let re;
  await axios.post(url, latLon, { headers: header })
    .then(function (res) {
      if (res && res.data.code == 0 && res.data.data) {
        re = res.data.data.alert;
      }
      // console.log(response.data);
    })
    .catch(function (error) {
      console.log("---------------getAlert start-------------------");
      console.log(error);
      console.log("---------------getAlert end-------------------");
      re = {};
    })
    return re;
}

exports.getTempOnTime = getTempOnTime;
exports.getAQI = getAQI;
exports.getIndex = getIndex;
exports.getTwentyFourHours = getTwentyFourHours;
exports.getAlert = getAlert;
